'use strict'

const { Service } = require('egg')
const COS = require('cos-nodejs-sdk-v5')
const sendToWormhole = require('stream-wormhole')

class cos extends Service {
    async upload(name, body) {
        const { app } = this
        const cos = new COS({
            SecretId: 'AKIDPbXDdnqtqXyB8z9vymKdgSyDCvPcqIl4',
            SecretKey: 'C8pGabOvCDxRfdKlwsY2JMBl7F9pGTRA',
        })
        const upload = await app.promisify(cos.sliceUploadFile, cos)
        let result
        try {
            await upload({
                Bucket: 'kujomiva-1256415213',
                Region: 'ap-chengdu',
                Key: name,
                FilePath: body
            }).then(data => {
                result = data
            })
        } catch (err) {
            await sendToWormhole(body)
            result = err
        }

        return result
    }
}

module.exports = cos